# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=pcsx2
pkgver=1.7.4819
pkgrel=4
_fastfloat=v5.2.0
_glslang=11.12.0
_gtest=v1.13.0
_libchdr=54bfb871ccae31903b95a8feb7f2bf7121f304be
_rcheevos=v10.7.1
_patches=8519fdb0b28672cc4fd6ff3cacca5133a8e310bc
pkgdesc="Playstation 2 emulator"
url="https://github.com/PCSX2/pcsx2"
# upstream limits
arch="x86_64"
license="GPL-3.0-or-later AND LGPL-3.0-or-later"
makedepends="
	alsa-lib-dev
	clang
	cmake
	curl-dev
	eudev-dev
	extra-cmake-modules
	ffmpeg-dev
	glslang-dev
	libaio-dev
	libpcap-dev
	libpng-dev
	libzip-dev
	lld
	llvm
	pulseaudio-dev
	qt6-qtbase-dev
	qt6-qttools-dev
	rapidyaml-dev
	samurai
	sdl2-dev
	vulkan-headers
	vulkan-loader-dev
	wayland-dev
	zip
	zstd-dev
	"
checkdepends="perl"
source="https://github.com/PCSX2/pcsx2/archive/refs/tags/v$pkgver/pcsx2-v$pkgver.tar.gz
	fastfloat-$_fastfloat.tar.gz::https://github.com/fastfloat/fast_float/archive/$_fastfloat.tar.gz
	glslang-$_glslang.tar.gz::https://github.com/KhronosGroup/glslang/archive/$_glslang.tar.gz
	gtest-$_gtest.tar.gz::https://github.com/google/googletest/archive/$_gtest.tar.gz
	libchdr-$_libchdr.tar.gz::https://github.com/rtissera/libchdr/archive/$_libchdr.tar.gz
	rcheevos-$_rcheevos.tar.gz::https://github.com/RetroAchievements/rcheevos/archive/$_rcheevos.tar.gz
	pcsx2_patches-$_patches.tar.gz::https://github.com/PCSX2/pcsx2_patches/archive/$_patches.tar.gz
	fmt-9.1.0.tar.gz::https://github.com/fmtlib/fmt/archive/refs/tags/9.1.0.tar.gz
	fast-float.patch
	fix-lfs64.patch
	unbundle-vulkan.patch
	ryml.patch
	version.patch
	zstd.patch

	PCSX2.desktop
	"

prepare() {
	default_prepare

	rmdir 3rdparty/glslang/glslang
	rmdir 3rdparty/gtest
	rmdir 3rdparty/libchdr/libchdr
	rmdir 3rdparty/rcheevos/rcheevos
	rmdir 3rdparty/fmt/fmt

	rm -r 3rdparty/ffmpeg

	ln -sfv "$srcdir"/glslang-${_glslang/v/} \
		"$builddir"/3rdparty/glslang/glslang
	ln -sfv "$srcdir"/googletest-${_gtest/v/} \
		"$builddir"/3rdparty/gtest
	ln -sfv "$srcdir"/libchdr-$_libchdr \
		"$builddir"/3rdparty/libchdr/libchdr
	ln -sfv "$srcdir"/rcheevos-${_rcheevos/v/} \
		"$builddir"/3rdparty/rcheevos/rcheevos

	ln -sfv "$srcdir"/fast_float-${_fastfloat/v/} \
		"$builddir"/3rdparty/fast_float

	ln -sfv "$srcdir"/fmt-9.1.0 \
		"$builddir"/3rdparty/fmt/fmt
	sed -i \
		-e "s|@@VERSION@@|$pkgver|g" \
		-e "s|@@PKGREL@@|$pkgrel|g" \
		cmake/Pcsx2Utils.cmake
}

build() {
	export CC=clang
	export CXX=clang++
	export CFLAGS="$CFLAGS -flto=thin"
	export CXXFLAGS="$CXXFLAGS -flto=thin -Wno-macro-redefined -Wno-deprecated-declarations"
	export LDFLAGS="$LDFLAGS -fuse-ld=lld -Wl,--icf=safe,--gc-sections"

	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_DISABLE_PRECOMPILE_HEADERS=ON \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DDISABLE_ADVANCE_SIMD=ON \
		-DDISABLE_BUILD_DATE=ON \
		-DENABLE_TESTS="$(want_check && echo ON || echo OFF)" \
		-DCUBEB_API=ON \
		-DUSE_ACHIEVEMENTS=ON \
		-DUSE_DISCORD_PRESENCE=OFF \
		-DUSE_LINKED_FFMPEG=ON \
		-DUSE_SYSTEM_LIBS=ON \
		-DUSE_VTUNE=OFF \
		-DUSE_VULKAN=ON \
		-DQT_BUILD=ON \
		-DWAYLAND_API=ON \
		-DX11_API=ON \
		-DUSE_SYSTEM_FMT=OFF

	cmake --build build --target all "$(want_check && echo unittests)"

	cd "$srcdir"/pcsx2_patches-$_patches/patches/
	zip -9 "$builddir"/patches.zip ./*.pnach
}

check() {
	ctest --test-dir build/tests/ctest --output-on-failure
}

package() {
	install -Dm644 bin/resources/icons/AppIconLarge.png \
		"$pkgdir"/usr/share/icons/hicolor/256x256/apps/PCSX2.png
	install -Dm644 "$srcdir"/PCSX2.desktop \
		-t "$pkgdir"/usr/share/applications/

	mkdir -p "$pkgdir"/usr/lib/

	rm -f build/bin/*test*
	cp -a build/bin "$pkgdir"/usr/lib/PCSX2

	install -Dm644 patches.zip -t "$pkgdir"/usr/lib/PCSX2/resources/

	install -Dm755 /dev/stdin "$pkgdir"/usr/bin/pcsx2 <<-EOF
		#!/bin/sh
		exec /usr/lib/PCSX2/pcsx2-qt "\$@"
	EOF
}

sha512sums="
2aee3f634e866561fa1c4a54e2f209df7fdc98585ba42de9d0e833fd2c261615c354dfae212b46fe99a97b3766362e43fd15ffb33d7f57e428d8e00715e35da3  pcsx2-v1.7.4819.tar.gz
c703c7cba3c69775317c66a62ce145646fd7d3d063124501e3d6a7deebb8c62c14a2ccdffed18de2d73d9d3a8ba2061ef1d34cc780ee0b6d607935d5f1b1de81  fastfloat-v5.2.0.tar.gz
fd955f9912551668056dfe52835eef11e5dc0bf0d25b2d961a31f684adbd63bc6380759944c1921cfd63d359a58c7cc3a4a4d5eea69fa1b050f58960e5101271  glslang-11.12.0.tar.gz
70c0cfb1b4147bdecb467ecb22ae5b5529eec0abc085763213a796b7cdbd81d1761d12b342060539b936fa54f345d33f060601544874d6213fdde79111fa813e  gtest-v1.13.0.tar.gz
ce993e00b78e972bb1c5745594213254966ca1ae43cc11917e22355d95c056e4f6b067f201cbeadce9ba75567063e45d459ed2564acb72fec5db4c0708f751b1  libchdr-54bfb871ccae31903b95a8feb7f2bf7121f304be.tar.gz
65c1c65328974afb21962ed0a6b73702b0eeefb93d3ca3a1d1d517864cc71f6d154578bd19df153da871791b5bd511e69f7f4db1a2178e14e9bc0df7d25b17e9  rcheevos-v10.7.1.tar.gz
a31b379639b1db97c08034004fe1f2d1ca66b6bbce3c5688da487b2028303bb6fcbc8229f064e132cf18a711ac6248805b8ce6368305cc302a7956ca70355a38  pcsx2_patches-8519fdb0b28672cc4fd6ff3cacca5133a8e310bc.tar.gz
a18442042722dd48e20714ec034a12fcc0576c9af7be5188586970e2edf47529825bdc99af366b1d5891630c8dbf6f63bfa9f012e77ab3d3ed80d1a118e3b2be  fmt-9.1.0.tar.gz
20624d3e46b956a5f0339b569891824b7a9321f4395c572393d42f7520d93b30b6e6bb8af90b5d6c52cbcaf2d82eb386bec3943532c5f1d8303c8b50938cd89b  fast-float.patch
e6e7964e36274372c3b9c67acd66a29b8ae7f4be1503959deb8284d427cddb5ea5bb534aad35f53073e8983bcb319d90b1127fe5dd6b8bd6bb1f3f8f306fcf0c  fix-lfs64.patch
461f184cc3b5c76539247db66fb0a364edbddc3d434869ff1641e723c6d962115cfa190be5e95205f31108ed47be0db6abf54c92d9875589ac978e73a29a379c  unbundle-vulkan.patch
151db2f789bd19937b7c78ac883388d672af0f5c38693b187311dd43d699703a8b36d9ace6d8db40e19d6cb8549a9b14a0842c4217c7eddfd547fc61f05c045c  ryml.patch
113f0e80c7975985d65c90acd2b4c9a4487d0c9f240178500feac512808b836d6ae36343895a4ef4f16d91364eeee4f0a3aa4ecc17708facdaf0cb430b5e9a71  version.patch
8a55a976188f53d8cab18a78cecbc2d34692411801126fb9432874ba26ef0245819d2980355beed4763b8496a2497330902ad5de7c288a3acb8837b305fe4a04  zstd.patch
bdc23c2773e1b963b06c0a51e3685b0a86db7dfe5cb2dd3e0cad4dd6f72aab2abef7eb421c8fb0f5303fae5c7ffb265eaa3a6e470998943aed54527bbb4d676c  PCSX2.desktop
"
